/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package swt.screen;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import swt.commons.Const;

import com.swtdesigner.SWTResourceManager;

/**
 * @author Thuc
 *
 */
public class DigitalClock extends Composite implements Runnable {

	private Composite parent;
	private CLabel lblClock;
	private static final int interval = 1000; // time in milliseconds

	private static int SIZE_X = 160;
	private static int SIZE_Y = 25;
	private static Font FONT = SWTResourceManager.getFont("Microsoft Sans Serif", 10, SWT.BOLD);
	private static Color BACKGROUND_COLOR = SWTResourceManager.getColor(0, 0, 0);
	private static Color FOREGROUND_COLOR = SWTResourceManager.getColor(102, 255, 255); 
	
	private boolean border = false;
	private boolean displayDateFlag = true;
	private boolean displayTimeFlag = true;
	private String displayFormatPattern = Const.DATE_FORMAT + "  < " + Const.TIME_FORMAT_24 + " >";
	
	/**
	 * Constructor
	 * @param parent
	 * @param style
	 */
	public DigitalClock(Shell parent, int style){
		super(parent, style);
		this.parent = parent;
	}
	
	/**
	 * Constructor
	 * @param parent
	 * @param style
	 * @param size 
	 * @param font 
	 * @param background 
	 * @param foreground 
	 */
	public DigitalClock(Composite parent, int style, Point size, Font font,
			Color background, Color foreground) {
		super(parent, style);

		this.parent = parent;
		if (size != null){
			SIZE_X = size.x;
			SIZE_Y = size.y;
		}
		if (font != null){
			FONT = font;
		}
		if (background != null){
			BACKGROUND_COLOR = background;
		}
		if (foreground != null){
			FOREGROUND_COLOR = foreground;
		}
	}
	
	/**
	 * start clock
	 *
	 */
	public void start(){
		initialize();
		Thread thread = new Thread(this);
		thread.start();
	}
	
	/**
	 * initialize
	 *
	 */
	private void initialize() {
		
		if (!border){
			lblClock = new CLabel(this, SWT.CENTER);
		}else{
			lblClock = new CLabel(this, SWT.CENTER | SWT.BORDER);
		}
		lblClock.setBounds(0, 0, SIZE_X, SIZE_Y);
		lblClock.setFont(FONT);
		lblClock.setBackground(BACKGROUND_COLOR);
		lblClock.setForeground(FOREGROUND_COLOR);
		lblClock.setText("Digital Clock");
		
		if (!displayDateFlag) {
			displayFormatPattern = Const.TIME_FORMAT_24;
		}
		if (!displayTimeFlag) {
			displayFormatPattern = Const.DATE_FORMAT;
		}
		if (!displayDateFlag && !displayTimeFlag) {
			throw new NullPointerException("Should display Date or Time.");
		}
	}

	/**
	 * run
	 *
	 */
	public void run (){
		final DateFormat dateFormat = new SimpleDateFormat(displayFormatPattern);
		while (true) { // gets data for the clock
			if (!this.isDisposed()){
				parent.getDisplay().asyncExec(new Runnable() {
					public void run() {
						// Update UI.
						Date time = new Date();
						lblClock.setText(dateFormat.format(time));
					}
				});
				try { // thread sleeps for a sec
					Thread.sleep(interval);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if (this.parent.isDisposed()){
				this.dispose();
				Thread.interrupted();
				return;
			}
		}
	}
	
	/**
	 * setBorder true/false
	 * @param flag
	 */
	public void setBorder(boolean flag){
		this.border = flag;
	}

	/**
	 * @param displayDateFlag the displayDateFlag to set
	 */
	public void setDisplayDateFlag(boolean displayDateFlag) {
		this.displayDateFlag = displayDateFlag;
	}

	/**
	 * @param displayTimeFlag the displayTimeFlag to set
	 */
	public void setDisplayTimeFlag(boolean displayTimeFlag) {
		this.displayTimeFlag = displayTimeFlag;
	}
}
